//
//  PPAlbumPictureDescriptionViewController.m
//  pickpic
//
//  Created by Jinxiao on 9/29/16.
//  Copyright © 2016 MicroDream. All rights reserved.
//

#import "PPAlbumPictureDescriptionViewController.h"

@interface PPAlbumPictureDescriptionViewController () <UITextViewDelegate>
@property (readwrite, nonatomic, strong) DGPlaceholderTextView *textView;
@property (readwrite, nonatomic, strong) UILabel *countLabel;
@property (readwrite, nonatomic, assign) CGFloat keyboardTop;
@property (readwrite, nonatomic, strong) NSString *desc;
@end

@implementation PPAlbumPictureDescriptionViewController

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    _keyboardTop = self.view.height;

    self.title = @"图片描述";

//    __weak typeof(self) weakSelf = self;

    
    [self setupBackItem];
    
//    [self setNavigationRightTitle:@"完成" titleColor:[UIColor colorWithRGBHex:0xffdd17] action:^{
//        [weakSelf saveItemDidPressed];
//    }];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(saveItemDidPressed)];
    self.navigationItem.rightBarButtonItem.tintColor = self.artwork.desc.length > 0?COLORA(0, 118, 255):COLORA(179, 214, 255);

    _textView = [[DGPlaceholderTextView alloc] init];
    _textView.placeholder = @"请输入文字";
    _textView.placeholderColor = [UIColor colorWithRGBHex:0xb2b2b2];
//    _textView.textContainerInset = UIEdgeInsetsMake(15, 15, 40, 15);
    _textView.textContainerInset = UIEdgeInsetsMake(26, 14, 26, 14);
    _textView.font = [UIFont systemFontOfSize:16];
    _textView.delegate = self;
    _textView.returnKeyType = UIReturnKeyDone;
    [self.view addSubview:_textView];

    _countLabel = [[UILabel alloc] init];
    _countLabel.font = [UIFont systemFontOfSize:14];
    _countLabel.textColor = [UIColor colorWithRGBHex:0xb2b2b2];
    _countLabel.textAlignment = NSTextAlignmentRight;
    [self.view addSubview:_countLabel];

    _textView.text = _desc;

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveKeyboardWillChangeFrameNotification:) name:UIKeyboardWillChangeFrameNotification object:nil];

    [self updateWordCount];
}

- (void)didReceiveKeyboardWillChangeFrameNotification:(NSNotification *)notification {
    CGRect endRect = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    NSTimeInterval duration = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    UIViewAnimationCurve curve = [[notification.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];

    CGRect rect = [[[UIApplication sharedApplication] keyWindow] convertRect:endRect toView:self.view];

    _keyboardTop = CGRectGetMinY(rect);

    [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState|(curve << 16) animations:^{
        _textView.height = _keyboardTop;
        _countLabel.bottom = _textView.bottom - 8;
    } completion:nil];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    _textView.frame = CGRectMake(0, 0, self.view.width, _keyboardTop);
    _countLabel.frame = CGRectMake(8, _textView.bottom - 8 - 20, self.view.width - 16, 20);
}

- (void)saveItemDidPressed {
    [_delegate descriptionViewController:self didSaveDescription:_textView.text];

    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setArtwork:(PPAbstractArtwork *)artwork {
    _artwork = artwork;

    _desc = artwork.desc;
    
    self.navigationItem.rightBarButtonItem.tintColor = _desc.length > 0?COLORA(0, 118, 255):COLORA(179, 214, 255);
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }

    return YES;
}

- (void)textViewDidChange:(UITextView *)textView {
 
    self.navigationItem.rightBarButtonItem.tintColor = textView.text.length > 0?COLORA(0, 118, 255):COLORA(179, 214, 255);
    
    _desc = textView.text;

    [self updateWordCount];
}
- (void)updateWordCount {
    NSInteger count = _desc.charCount;

    _countLabel.text = [NSString stringWithFormat:@"%li/80", count];

    UIBarButtonItem *saveItem = self.navigationItem.rightBarButtonItem;

    if(count > 80) {
        _countLabel.textColor = [UIColor colorWithRGBHex:0xdf1921];
        saveItem.enabled = NO;
    } else {
        _countLabel.textColor = [UIColor colorWithRGBHex:0xb2b2b2];
        saveItem.enabled = YES;
    }
}

@end
